package com.qf.pay.app.controller;

import com.qf.pay.app.pojo.AppOrder;
import com.qf.pay.app.pojo.AppUser;
import com.qf.pay.app.service.AppOrderService;
import com.qf.pay.app.util.HttpUtil;
import com.qf.pay.app.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/order")
public class AppOrderController {

    @Autowired
    private AppOrderService appOrderService;

    @RequestMapping("/pay")
    public String pay(Integer price, HttpSession session, Model model) {
        AppUser appUser = (AppUser) session.getAttribute("SESSION_USER");
        if (appUser == null) {
            return "/login.jsp";
        }
        String orderId = "pay" + System.currentTimeMillis();
        price *= 100;//把元变成分
        AppOrder order = new AppOrder();
        order.setOrderId(orderId);
        order.setPrice(price);
        order.setUserId(appUser.getId());
        order.setOtherParam("discount=500");
        appOrderService.addOrder(order);
        StringBuilder sb = new StringBuilder();
        sb.append("orderId=").append(order.getOrderId());
        sb.append("price=").append(order.getPrice());
        sb.append("merchantId=").append(1);
        sb.append("notifyURL=").append("http://localhost:8081/order/callback");
        sb.append("otherParam=");
        if (!StringUtils.isEmpty(order.getOtherParam())) {
            sb.append(order.getOtherParam());
        }
        String secretKey = "1801v587";
        sb.append(secretKey);
        //自己计算出的签名
        String signStr = MD5Util.getMD5(sb.toString());
        sb = new StringBuilder();
        sb.append("orderId=").append(order.getOrderId());
        sb.append("&price=").append(order.getPrice());
        sb.append("&merchantId=").append(1);
        sb.append("&notifyURL=").append("http://localhost:8081/order/callback");
        sb.append("&otherParam=");
        if (!StringUtils.isEmpty(order.getOtherParam())) {
            sb.append(order.getOtherParam());
        }
        sb.append("&sign=").append(signStr);
        String res = HttpUtil.doGet("http://localhost:8080/order/create?" + sb.toString());
        System.out.println(res);
        model.addAttribute("result", res);
        return "/index.jsp";
    }

    @RequestMapping("/callback")
    @ResponseBody
    public String callback(AppOrder order, String sign) {
        StringBuilder sb = new StringBuilder();
        sb.append("orderId=").append(order.getOrderId());
        sb.append("&tradeNum=").append(order.getTradeNum());
        sb.append("&price=").append(order.getPrice());
        sb.append("&merchantId=").append(1);
        sb.append("&otherParam=");
        if (!StringUtils.isEmpty(order.getOtherParam())) {
            sb.append(order.getOtherParam());
        }
        String secretKey = "1801v587";
        sb.append(secretKey);
        //自己计算出的签名
        String signStr = MD5Util.getMD5(sb.toString());
        if (sign.equals(signStr)) {
            AppOrder localOrder = appOrderService.getOrderById(order.getOrderId());
            if(localOrder.getPayStatus().equals(1)){
                return "ok";
            }
            localOrder.setPrice(order.getPrice());
            localOrder.setPayStatus(1);
            appOrderService.updateOrder(localOrder);
            return "ok";
        }
        return "error";
    }
}
